﻿using System.Windows.Input;
using Microsoft.Practices.Prism.Commands;
using YunBox2.Contracts;
using System.Windows;
using YunBox2.Contracts.Core;

namespace YunBox2.Client.Infrastructure.ViewModels
{
    public class CompanyProfileViewModel : DomainObject
    {
        public CompanyProfileViewModel()
        {
            _bank = _companyProfile.Bank;
            _bankAccount = _companyProfile.BankAccount;
            _lawPerson = _companyProfile.LawPerson;
            _name = _companyProfile.Name;
            _shortName = _companyProfile.ShortName;

            _saveCommand = new DelegateCommand(Save);
            _cancelCommand = new DelegateCommand(Cancel);
        }

        #region Command

        private readonly DelegateCommand _cancelCommand;
        private readonly DelegateCommand _saveCommand;

        public ICommand SaveCommand
        {
            get
            {
                return _saveCommand;
            }
        }

        public ICommand CancelCommand
        {
            get
            {
                return _cancelCommand;
            }
        }

        private void Save()
        {
            _companyProfile.Bank = _bank;
            _companyProfile.BankAccount = _bankAccount;
            _companyProfile.LawPerson = _lawPerson;
            _companyProfile.Name = _name;
            _companyProfile.ShortName = _shortName;
            try
            {
                ClientContext.Current.SystemService.CompanyProfileToUpdate(ClientContext.Current.ClientID,
                                                                           _companyProfile);
                ClientContext.Current.Company =
                    ClientContext.Current.SystemService.CompanyProfileToGetProfile(ClientContext.Current.ClientID);

                RaisePropertyChanged("Bank", "BankAccount", "LawPerson", "Name", "ShortName", "HasChanged",
                                     "IsEnableSave", "IsEnableCancel");
            }
            catch
            {
                MessageBox.Show("保存失败!");
            }
        }

        private void Cancel()
        {
            if (!IsEnableCancel)
            {
                return;
            }
            _bank = _companyProfile.Bank;
            _bankAccount = _companyProfile.BankAccount;
            _lawPerson = _companyProfile.LawPerson;
            _name = _companyProfile.Name;
            _shortName = _companyProfile.ShortName;
            RaisePropertyChanged("Bank", "BankAccount", "LawPerson", "Name", "ShortName", "HasChanged", "IsEnableSave",
                                 "IsEnableCancel");
        }

        #endregion

        #region 状态

        public bool HasChanged
        {
            get
            {
                return _companyProfile.Name != _name && _companyProfile.ShortName != _shortName &&
                       _companyProfile.LawPerson != _lawPerson && _companyProfile.Bank != _bank &&
                       _companyProfile.BankAccount != _bankAccount;
            }
        }

        public bool IsEnableSave
        {
            get
            {
                return HasChanged && !HasError;
            }
        }

        public bool IsEnableCancel
        {
            get
            {
                return !HasChanged;
            }
        }

        #endregion

        #region 数据

        private string _bank;
        private string _bankAccount;
        private string _lawPerson;
        private string _name;
        private string _shortName;

        private CompanyProfile _companyProfile
        {
            get
            {
                return ClientContext.Current.Company;
            }
        }

        public string Name
        {
            get
            {
                return _name;
            }
            set
            {
                if (_name != value)
                {
                    _name = value;
                    RaisePropertyChanged("Name", "HasChanged");
                }
            }
        }

        public string ShortName
        {
            get
            {
                return _shortName;
            }
            set
            {
                if (_shortName != value)
                {
                    _shortName = value;
                    RaisePropertyChanged("ShortName", "HasChanged");
                }
            }
        }

        /// <summary>
        /// 法人代表
        /// </summary>
        public string LawPerson
        {
            get
            {
                return _lawPerson;
            }
            set
            {
                if (_lawPerson != value)
                {
                    _lawPerson = value;
                    RaisePropertyChanged("LawPerson", "HasChanged");
                }
            }
        }

        public string Bank
        {
            get
            {
                return _bank;
            }
            set
            {
                if (_bank != value)
                {
                    _bank = value;
                    RaisePropertyChanged("Bank", "HasChanged");
                }
            }
        }

        public string BankAccount
        {
            get
            {
                return _bankAccount;
            }
            set
            {
                if (_bankAccount != value)
                {
                    _bankAccount = value;
                    RaisePropertyChanged("BankAccount", "HasChanged");
                }
            }
        }

        #endregion
    }
}